/**
 * @(#)ClientHandler.java, Sep 13, 2012. 
 * 
 * Copyright 2012 Yodao, Inc. All rights reserved.
 * YODAO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.buaaer.mina;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

/**
 *
 * @author wangheng
 *
 */
public class ClientHandler extends IoHandlerAdapter {
    
    private Client client;
    
    public ClientHandler(Client client) {
        this.client  = client;
    }
    
    public void sessionCreated(IoSession session) throws Exception {
        System.out.println(session.getId() + " create(client)");
    }

    public void sessionOpened(IoSession session) throws Exception {
        System.out.println(session.getId() + " open(client)");
    }

    public void sessionClosed(IoSession session) throws Exception {
        System.out.println(session.getId() + " close(client)");
        client.close();
        System.out.println("");
    }
    
    
    @Override
    public void messageReceived(IoSession session, Object message)
            throws Exception {
        
        System.out.print("recv from server:");
        DataPackage data = (DataPackage)message;
        System.out.println(data);
        long seqId = data.getSeqId();
        FutureResult object = this.client.getContextMap().remove(seqId);
        object.finish(data);
    }
    

    
}